//
//  JTNavigationController.m
//  JTNavigationController
//
//  Created by Tian on 16/1/23.
//  Copyright © 2016年 TianJiaNan. All rights reserved.
//

#import "JTWrapNavigationController.h"
#import "JTNavigationController.h"
#import "UIViewController+JTNavigationExtension.h"

@implementation JTWrapNavigationController

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    JTNavigationController * jt_navigationController = self.navigationController;
    viewController.jt_navigationController = jt_navigationController;
    viewController.jt_fullScreenPopGestureEnabled = jt_navigationController.fullScreenPopGestureEnabled;
    UIImage * backButtonImage = jt_navigationController.backButtonImage;
    if (!backButtonImage) {
        backButtonImage = [UIImage imageNamed:kDefaultBackImageName];
    }
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTouchUpInside)];
    [self.navigationController pushViewController:[JTWrapViewController wrapViewControllerWithViewController:viewController] animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    return [self.navigationController popViewControllerAnimated:animated];
}

- (NSArray<UIViewController *>*)popToRootViewControllerAnimated:(BOOL)animated {
    return [self.navigationController popToRootViewControllerAnimated:animated];
}

- (NSArray<UIViewController *>*)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    JTNavigationController * jt_navigationController = viewController.jt_navigationController;
    NSInteger index = [jt_navigationController.jt_viewControllers indexOfObject:viewController];
    return [self.navigationController popToViewController:jt_navigationController.viewControllers[index] animated:animated];
}

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void(^)(void))completion {
    [self.navigationController dismissViewControllerAnimated:flag completion:completion];
    self.viewControllers.firstObject.jt_navigationController = nil;
}

- (void)backButtonTouchUpInside {
    [self.navigationController popViewControllerAnimated:YES];
}

@end

